import { getSecond, uuid } from '../../utils/common'
import { _sendVerificationCode , _checkVerificationCode } from '../../utils/sendCode'
import { ManageAdmin , Account , ProvePersonal , ProveCompany} from '../../models/'
import { CODE , SEND_TIME , TOKEN_TIME , VERIFICATION_CODE_TYPE , UID_KEY , _getCountLimit , PROGRESS_STATUS} from '../../config/config'
import { _getAccount , _bindPhone } from '../../utils/account' 

/**
 * @api {post} /manage_prove/personalList 获取个人认证列表
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName personalList
 * @apiGroup z_manage_prove
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/apimanage_prove/personalList'

 * @apiParam {String}  token                   管理员token
 * @apiParam {Integer}  page                   页码 1开始
 * @apiParam {Integer}  limit                  每页显示条数 默认50
 * @apiParam {Integer}  status                 认证进度
 *
 *
 * @apiSuccess {Array}   lit                认证信息集合
 * 
 * @apiSuccess {Object}   prove             认证信息
 * @apiSuccess {String}   id                认证id
 * @apiSuccess {String}    uid              用户id
 * @apiSuccess {String}   name              姓名
 * @apiSuccess {String}   idcard            身份证
 * @apiSuccess {String}   handle_code       处理码
 * @apiSuccess {String}   img1              身份证正面
 * @apiSuccess {String}   img2              身份证背面
 * @apiSuccess {String}   status            审核状态 进度 1.申请中 2.通过认证 3.取消认证 4.认证失败 99.删除
 * @apiSuccess {String}   remark            备注
 * @apiSuccess {String}   ext               拓展信息
 * @apiSuccess {String}   ctime             创建时间
 * @apiSuccess {String}   htime             处理时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_prove/personalList
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function personalList(ctx) {
    let page  	           = ctx.request.body.page
	const status           = ctx.request.body.status
	if(!page) {
		page               = 0
	}
	page                   = Number(page) - 1
	const countLimit       = _getCountLimit(ctx.request.body.limit)
	const where            = {}
	const order			   = [['ctime', 'DESC']]
	const limit			   = [page * countLimit,countLimit]
	const query            = {where,order,limit}
	if(status) {
		where.status       = status
	} else {
		where.status       = { $not:PROGRESS_STATUS.DELETE }
	}
	const proves           = await ProvePersonal.findAll(query)
	const count            = await ProvePersonal.count({where})

	ctx.body               = CODE.success({
		lit:proves,count
	})
}
/**
 * @api {post} /manage_prove/completePersonal 通过个人认证
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName completePersonal
 * @apiGroup z_manage_prove
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/apimanage_prove/completePersonal'

 * @apiParam {String}  token                   管理员token
 * @apiParam {String}  hash_code               处理码
 * 
 * @apiSuccess {Object}   prove             认证信息
 * @apiSuccess {String}   id                认证id
 * @apiSuccess {String}    uid              用户id
 * @apiSuccess {String}   name              姓名
 * @apiSuccess {String}   idcard            身份证
 * @apiSuccess {String}   handle_code       处理码
 * @apiSuccess {String}   img1              身份证正面
 * @apiSuccess {String}   img2              身份证背面
 * @apiSuccess {String}   status            审核状态 进度 1.申请中 2.通过认证 3.取消认证 4.认证失败 99.删除
 * @apiSuccess {String}   remark            备注
 * @apiSuccess {String}   ext               拓展信息
 * @apiSuccess {String}   ctime             创建时间
 * @apiSuccess {String}   htime             处理时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_prove/completePersonal
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function completePersonal(ctx) {

	const hash_code			    = ctx.request.body.hash_code
	const provePersonal         = await ProvePersonal.findOne({where:{hash_code}})
	if(normalWork) {
		provePersonal.status    = PROGRESS_STATUS.COMPLETE
		provePersonal.htime     = getSecond()
		provePersonal.hash_code = uuid()
		await provePersonal.save()
		ctx.body                = CODE.success({prove:provePersonal})
		return
	}
	ctx.body                    = CODE.MANAGE_ERROR.ERROR_HASH_CODE
}

/**
 * @api {post} /manage_prove/failPersonal 不通过个人认证
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName failPersonal
 * @apiGroup z_manage_prove
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/apimanage_prove/failPersonal'

 * @apiParam {String}  token                   管理员token
 * @apiParam {String}  hash_code               处理码
 * 
 * @apiSuccess {Object}   prove             认证信息
 * @apiSuccess {String}   id                认证id
 * @apiSuccess {String}    uid              用户id
 * @apiSuccess {String}   name              姓名
 * @apiSuccess {String}   idcard            身份证
 * @apiSuccess {String}   handle_code       处理码
 * @apiSuccess {String}   img1              身份证正面
 * @apiSuccess {String}   img2              身份证背面
 * @apiSuccess {String}   status            审核状态 进度 1.申请中 2.通过认证 3.取消认证 4.认证失败 99.删除
 * @apiSuccess {String}   remark            备注
 * @apiSuccess {String}   ext               拓展信息
 * @apiSuccess {String}   ctime             创建时间
 * @apiSuccess {String}   htime             处理时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_prove/failPersonal
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function failPersonal(ctx) {
	const hash_code			    = ctx.request.body.hash_code
	const provePersonal         = await ProvePersonal.findOne({where:{hash_code}})
	if(normalWork) {
		provePersonal.status    = PROGRESS_STATUS.FAIL
		provePersonal.htime     = getSecond()
		provePersonal.hash_code = uuid()
		await provePersonal.save()
		ctx.body                = CODE.success({prove:provePersonal})
		return
	}
	ctx.body                    = CODE.MANAGE_ERROR.ERROR_HASH_CODE
}

/**
 * @api {post} /manage_prove/queryPersonalByUid 通过用户id找到个人认证信息
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName queryPersonalByUid
 * @apiGroup z_manage_prove
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/apimanage_prove/queryPersonalByUid'

 * @apiParam {String}  token                管理员token
 * @apiParam {String}  uid                  用户id
 * 
 * @apiSuccess {Object}   prove             认证信息
 * @apiSuccess {String}   id                认证id
 * @apiSuccess {String}   uid               用户id
 * @apiSuccess {String}   name              姓名
 * @apiSuccess {String}   idcard            身份证
 * @apiSuccess {String}   handle_code       处理码
 * @apiSuccess {String}   img1              身份证正面
 * @apiSuccess {String}   img2              身份证背面
 * @apiSuccess {String}   status            审核状态 进度 1.申请中 2.通过认证 3.取消认证 4.认证失败 99.删除
 * @apiSuccess {String}   remark            备注
 * @apiSuccess {String}   ext               拓展信息
 * @apiSuccess {String}   ctime             创建时间
 * @apiSuccess {String}   htime             处理时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_prove/queryPersonalByUid
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function queryPersonalByUid(ctx) {
	const uid			        = ctx.request.body.uid
	const provePersonal         = await ProvePersonal.findOne({where:{uid}})
	if(normalWork) {
		ctx.body                = CODE.success({prove:provePersonal})
		return
	}
	ctx.body                    = CODE.MANAGE_ERROR.ERROR_PROVE_NOT_FOUNT
}



/**
 * @api {post} /manage_prove/companyList 获取企业认证列表
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName companyList
 * @apiGroup z_manage_prove
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/apimanage_prove/companyList'

 * @apiParam {String}  token                   管理员token
 * @apiParam {Integer}  page                   页码 1开始
 * @apiParam {Integer}  limit                  每页显示条数 默认50
 * @apiParam {Integer}  status                 认证进度
 *
 *
 * @apiSuccess {Array}   lit                认证信息集合
 * 
 * @apiSuccess {Object}   prove             认证信息
 * @apiSuccess {String}   id                认证id
 * @apiSuccess {String}    uid              用户id
 * @apiSuccess {String}   name              姓名
 * @apiSuccess {String}   idcard            身份证
 * @apiSuccess {String}   handle_code       处理码
 * @apiSuccess {String}   img1              身份证正面
 * @apiSuccess {String}   img2              身份证背面
 * @apiSuccess {String}   status            审核状态 进度 1.申请中 2.通过认证 3.取消认证 4.认证失败 99.删除
 * @apiSuccess {String}   remark            备注
 * @apiSuccess {String}   ext               拓展信息
 * @apiSuccess {String}   ctime             创建时间
 * @apiSuccess {String}   htime             处理时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_prove/companyList
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function companyList(ctx) {
    let page  	           = ctx.request.body.page
	const status           = ctx.request.body.status
	if(!page) {
		page               = 0
	}
	page                   = Number(page) - 1
	const countLimit       = _getCountLimit(ctx.request.body.limit)
	const where            = {}
	const order			   = [['ctime', 'DESC']]
	const limit			   = [page * countLimit,countLimit]
	const query            = {where,order,limit}
	if(status) {
		where.status       = status
	} else {
		where.status       = { $not:PROGRESS_STATUS.DELETE }
	}
	const proves           = await ProveCompany.findAll(query)
	const count            = await ProveCompany.count({where})

	ctx.body               = CODE.success({
		lit:proves,count
	})
}
/**
 * @api {post} /manage_prove/completeCompany 通过企业认证
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName completeCompany
 * @apiGroup z_manage_prove
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/apimanage_prove/completeCompany'

 * @apiParam {String}  token                   管理员token
 * @apiParam {String}  hash_code               处理码
 * 
 * @apiSuccess {Object}   prove             认证信息
 * @apiSuccess {String}   id                认证id
 * @apiSuccess {String}    uid              用户id
 * @apiSuccess {String}   name              姓名
 * @apiSuccess {String}   idcard            身份证
 * @apiSuccess {String}   handle_code       处理码
 * @apiSuccess {String}   img1              身份证正面
 * @apiSuccess {String}   img2              身份证背面
 * @apiSuccess {String}   status            审核状态 进度 1.申请中 2.通过认证 3.取消认证 4.认证失败 99.删除
 * @apiSuccess {String}   remark            备注
 * @apiSuccess {String}   ext               拓展信息
 * @apiSuccess {String}   ctime             创建时间
 * @apiSuccess {String}   htime             处理时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_prove/completeCompany
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function completeCompany(ctx) {

	const hash_code			    = ctx.request.body.hash_code
	const proveCompany          = await ProveCompany.findOne({where:{hash_code}})
	if(normalWork) {
		proveCompany.status    = PROGRESS_STATUS.COMPLETE
		proveCompany.htime     = getSecond()
		proveCompany.hash_code = uuid()
		await proveCompany.save()
		ctx.body                = CODE.success({prove:proveCompany})
		return
	}
	ctx.body                    = CODE.MANAGE_ERROR.ERROR_HASH_CODE
}

/**
 * @api {post} /manage_prove/failCompany 不通过企业认证
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName failCompany
 * @apiGroup z_manage_prove
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/apimanage_prove/failCompany'

 * @apiParam {String}  token                   管理员token
 * @apiParam {String}  hash_code               处理码
 * 
 * @apiSuccess {Object}   prove             认证信息
 * @apiSuccess {String}   id                认证id
 * @apiSuccess {String}    uid              用户id
 * @apiSuccess {String}   name              姓名
 * @apiSuccess {String}   idcard            身份证
 * @apiSuccess {String}   handle_code       处理码
 * @apiSuccess {String}   img1              身份证正面
 * @apiSuccess {String}   img2              身份证背面
 * @apiSuccess {String}   status            审核状态 进度 1.申请中 2.通过认证 3.取消认证 4.认证失败 99.删除
 * @apiSuccess {String}   remark            备注
 * @apiSuccess {String}   ext               拓展信息
 * @apiSuccess {String}   ctime             创建时间
 * @apiSuccess {String}   htime             处理时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_prove/failCompany
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function failCompany(ctx) {
	const hash_code			   = ctx.request.body.hash_code
	const proveCompany         = await ProveCompany.findOne({where:{hash_code}})
	if(normalWork) {
		proveCompany.status    = PROGRESS_STATUS.FAIL
		proveCompany.htime     = getSecond()
		proveCompany.hash_code = uuid()
		await proveCompany.save()
		ctx.body                = CODE.success({prove:proveCompany})
		return
	}
	ctx.body                    = CODE.MANAGE_ERROR.ERROR_HASH_CODE
}

/**
 * @api {post} /manage_prove/queryCompanyByUid 通过用户id找到企业认证信息
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName queryCompanyByUid
 * @apiGroup z_manage_prove
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/apimanage_prove/queryCompanyByUid'

 * @apiParam {String}  token                管理员token
 * @apiParam {String}  uid                  用户id
 * 
 * @apiSuccess {Object}   prove             认证信息
 * @apiSuccess {String}   id                认证id
 * @apiSuccess {String}   uid               用户id
 * @apiSuccess {String}   name              姓名
 * @apiSuccess {String}   idcard            身份证
 * @apiSuccess {String}   handle_code       处理码
 * @apiSuccess {String}   img1              身份证正面
 * @apiSuccess {String}   img2              身份证背面
 * @apiSuccess {String}   status            审核状态 进度 1.申请中 2.通过认证 3.取消认证 4.认证失败 99.删除
 * @apiSuccess {String}   remark            备注
 * @apiSuccess {String}   ext               拓展信息
 * @apiSuccess {String}   ctime             创建时间
 * @apiSuccess {String}   htime             处理时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_prove/queryCompanyByUid
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function queryCompanyByUid(ctx) {
	const uid			       = ctx.request.body.uid
	const proveCompany         = await ProveCompany.findOne({where:{uid}})
	if(proveCompany) {
		ctx.body               = CODE.success({prove:proveCompany})
		return
	}
	ctx.body                   = CODE.MANAGE_ERROR.ERROR_PROVE_NOT_FOUNT
}




export async function checkToken(ctx,next) {
	const token  = ctx.request.body.token
	const admin  = await ManageAdmin.findOne({where:{uid:token,status:1}})
	if(admin) {
		ctx.body = { admin }
		if(next) {
			return next()
		}
	}
	ctx.body     = CODE.MANAGE_ERROR.ERROR_ADMIN_NOT_FOUND
}
/**
 * @apiDefine ERROR
 * @apiErrorExample {json} 错误返回:
 *     {
 *       "code": "错误码",
 *       "error": "错误描述",
 *       "msg":"错误信息"
 *     }
 */